package com.JobFind.logic.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.JobFind.logic.ILogLogic;
import com.JobFind.model.TbDeliver;
import com.JobFind.model.TbLog;
import com.JobFind.DAO.ICompanyDAO;
import com.JobFind.DAO.ILogDAO;
import com.JobFind.DAO.IUserDAO;
import com.JobFind.DTO.DeliverShowDTO;
import com.JobFind.DTO.LogShowDTO;

@Component
public class LogLogic implements ILogLogic{
	private ILogDAO  logDAO;
	@Resource
	private IUserDAO userDAO;
	@Resource
	private ICompanyDAO companyDAO;
	
	public ILogDAO getLogDAO() {
		return logDAO;
	}

	@Resource
	public void setLogDAO(ILogDAO logDAO) {
		this.logDAO = logDAO;
	}

	@Override
	@Transactional
	public Integer addLog(TbLog logBean) throws Exception{
		Integer result = 0;
		try {
			result = this.logDAO.addLog(logBean);
		} catch (Exception e) {
			e.printStackTrace();
			result = 0;
		}
		return result;
	}

	@Override
	@Transactional
	public String getLogInfo(String startTime, String endTime) throws Exception {
		String result = "";
		List<LogShowDTO> logShowDTOList = new ArrayList<LogShowDTO>();
		List<TbLog> LogList = null;
		try {
				LogList = this.logDAO.getLogInfo(startTime, endTime);
				if(LogList == null || LogList.size() == 0)
					result = "";
				else{
					for(TbLog log :LogList){
						logShowDTOList.add(log.toShowDTO());
					}
					for(int i=0;i<logShowDTOList.size();i++){
						if(logShowDTOList.get(i).getUserType().equals("个人")){
							logShowDTOList.get(i).setLoginName(this.userDAO.getUserLoinNameById(logShowDTOList.get(i).getUserId()));
						}else {
							logShowDTOList.get(i).setLoginName(this.companyDAO.getCompanyLoginNameById(logShowDTOList.get(i).getUserId()));
						}
					}
					result = JSONArray.fromObject(logShowDTOList).toString();
				}
				
		} catch (Exception e) {
		  e.printStackTrace();
		  result = "";
		}
		return result;
	}
}
